In [1]:
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
In [2]:
stop_search = pd.read_csv("D:/Stop_and_Search__Field_Interviews_.csv")
C:\Program Files\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2717: DtypeWarning: Columns (1,7,28) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)
In [3]:
stop_search.head()
Out[3]:
FieldInterviewID NOPD_Item EventDate District Zone OfficerAssignment StopDescription ActionsTaken VehicleYear VehicleMake ... SubjectWeight SubjectEyeColor SubjectHairColor SubjectDriverLicState CreatedDateTime LastModifiedDateTime Longitude Latitude Zip BlockAddress
0 17415 NaN 1/1/2010 1:11 6 E 6th District TRAFFIC VIOLATION NaN 2005.0 DODGE ... 160.0 Brown Black LA 1/1/2010 1:26 NaN 0.0 0.0 NaN NaN
1 17416 NaN 1/1/2010 2:06 5 D 5th District CALL FOR SERVICE NaN NaN NaN ... 140.0 Brown Black NaN 1/1/2010 2:27 NaN 0.0 0.0 NaN NaN
2 17416 NaN 1/1/2010 2:06 5 D 5th District CALL FOR SERVICE NaN NaN NaN ... 145.0 Brown Black NaN 1/1/2010 2:27 NaN 0.0 0.0 NaN NaN
3 17416 NaN 1/1/2010 2:06 5 D 5th District CALL FOR SERVICE NaN NaN NaN ... 140.0 Brown Black NaN 1/1/2010 2:27 NaN 0.0 0.0 NaN NaN
4 17416 NaN 1/1/2010 2:06 5 D 5th District CALL FOR SERVICE NaN NaN NaN ... 140.0 Brown Black NaN 1/1/2010 2:27 NaN 0.0 0.0 NaN NaN

5 rows × 29 columns

In [4]:
# check rows and columns
stop_search.shape
Out[4]:
(430157, 29)
In [5]:
# check datatype for each feature
print(stop_search.select_dtypes(include = ['float64']).dtypes)
print(stop_search.select_dtypes(include = ['int64']).dtypes)
print(stop_search.select_dtypes(include = ['object']).dtypes)
VehicleYear      float64
SubjectID        float64
SubjectAge       float64
SubjectHeight    float64
SubjectWeight    float64
Longitude        float64
Latitude         float64
Zip              float64
dtype: object
FieldInterviewID    int64
District            int64
dtype: object
NOPD_Item                object
EventDate                object
Zone                     object
OfficerAssignment        object
StopDescription          object
ActionsTaken             object
VehicleMake              object
VehicleModel             object
VehicleStyle             object
VehicleColor             object
SubjectRace              object
SubjectGender            object
SubjectHasPhotoID        object
SubjectEyeColor          object
SubjectHairColor         object
SubjectDriverLicState    object
CreatedDateTime          object
LastModifiedDateTime     object
BlockAddress             object
dtype: object
In [6]:
# check missing values for each feature
print("Total number of missing values are", stop_search.isnull().sum().sum())
miss_df = pd.DataFrame({'Column': stop_search.isnull().sum().index, 
                        'Num of Missing': stop_search.isnull().sum()}, index = None)
miss_df.set_index('Column', inplace = True)
miss_df = miss_df.rename_axis(None)
miss_df.sort_values(by = ['Num of Missing'], ascending = [False])
Total number of missing values are 2266160
Out[6]:
Num of Missing
LastModifiedDateTime 339539
ActionsTaken 277245
VehicleModel 224862
VehicleStyle 215180
VehicleYear 214003
VehicleColor 213187
VehicleMake 210589
SubjectDriverLicState 181838
Zip 109363
BlockAddress 96464
NOPD_Item 93550
SubjectAge 11277
SubjectHairColor 11277
SubjectEyeColor 11277
SubjectWeight 11277
SubjectHeight 11277
SubjectRace 11277
SubjectGender 11277
SubjectID 11277
OfficerAssignment 124
SubjectHasPhotoID 0
StopDescription 0
Zone 0
CreatedDateTime 0
District 0
Longitude 0
Latitude 0
EventDate 0
FieldInterviewID 0

Analysis purposes

(1) Demographics; (2) Vehicle information; (3) Locations; (4) Time; (5) Stop type; (6) Stop result.

2. Define the subject pictures

(1) Who has the high / lower probabilities of being stopped

3. Compare patterns of stop and search among districts and years

4. Individual level prediction analysis

(1) Make a good and accurate stop and search based on prediction; (2) Explore factors impacting a good stop and search.

(1) Demographics - SubjectRace, SubjectGender, SubjectAge, SubjectHeight, SubjectWeight, SubjectEyeColor, SubjectHairColor.

In [7]:
fig = plt.figure(dpi = 100, figsize = (30, 60))

ax1 = fig.add_subplot(4, 2, 1)
ax1.plot = sns.countplot(stop_search["SubjectRace"])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Race", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax2 = fig.add_subplot(4, 2, 2)
ax2.plot = sns.countplot(stop_search["SubjectGender"])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Gender", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax3 = fig.add_subplot(4, 2, 3)
ax3.plot = sns.distplot(stop_search["SubjectAge"][~ np.isnan(stop_search["SubjectAge"])], bins = 50)
sns.plt.xlim(0, 100)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Age", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

ax4 = fig.add_subplot(4, 2, 4)
ax4.plot = sns.distplot(stop_search["SubjectHeight"][~ np.isnan(stop_search["SubjectHeight"])], bins = 50)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
sns.plt.xlim(0, 100)
plt.xlabel("Height", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax5 = fig.add_subplot(4, 2, 5)
ax5.plot = sns.distplot(stop_search["SubjectWeight"][~ np.isnan(stop_search["SubjectWeight"])], bins = 50)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
sns.plt.xlim(0, 500)
plt.xlabel("Weight", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax7 = fig.add_subplot(4, 2, 6)
ax7.plot = sns.countplot(stop_search["SubjectEyeColor"])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Eye Color", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax8 = fig.add_subplot(4, 2, 7)
ax8.plot = sns.countplot(stop_search["SubjectHairColor"])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Hair Color", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax8 = fig.add_subplot(4, 2, 8)
ax8.plot = sns.countplot(stop_search["SubjectHasPhotoID"])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Has photo ID or not", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

fig.show()
C:\Program Files\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

(2) Vehicle information - VehicleModel, VehicleStyle, VehicleYear, VehicleColor, VehicleMake, SubjectDriverLicState.

In [8]:
# get frequency for uniques of each feature
print(stop_search["VehicleYear"].value_counts())
print(stop_search["VehicleModel"].value_counts())
print(stop_search["VehicleStyle"].value_counts())
print(stop_search["VehicleColor"].value_counts())
print(stop_search["VehicleMake"].value_counts())
print(stop_search["SubjectDriverLicState"].value_counts())
2006.0    16822
2005.0    15347
2004.0    14839
2002.0    14489
2003.0    14474
2001.0    13489
2000.0    13252
2007.0    13217
2008.0    11483
1999.0    10940
1998.0     7885
2010.0     7350
2011.0     6838
2009.0     6789
1997.0     6390
2012.0     6272
2013.0     5505
1996.0     4842
2014.0     4756
1995.0     4117
2015.0     3683
1994.0     2882
1993.0     1963
2016.0     1448
1992.0     1386
1900.0     1270
1991.0      981
1990.0      706
1989.0      524
1988.0      393
          ...  
1982.0       78
1979.0       75
1981.0       74
1978.0       47
1980.0       41
1975.0       35
1977.0       23
1976.0       18
1966.0       17
1971.0       13
1970.0       13
1972.0       12
1973.0       10
2018.0        9
1974.0        9
1967.0        8
1965.0        8
1969.0        6
1963.0        3
1957.0        3
1955.0        2
1901.0        2
2019.0        1
1968.0        1
1964.0        1
1959.0        1
1947.0        1
1937.0        1
1934.0        1
1902.0        1
Name: VehicleYear, dtype: int64
OTHER             54120
ACCORD             6538
ALTIMA             6352
CAMRY              5694
IMPALA             5620
PICKUP             5399
F150 PICKUP        5019
MAXIMA             3964
GRAND AM           3735
EXPLORER           3538
MALIBU             3493
TAHOE              3475
COROLLA            3424
CIVIC              3003
SILVERADO          2761
GRAND PRIX         2730
MONTE CARLO        2550
EXPEDITION         2337
SENTRA             2268
MUSTANG            2076
TAURUS             2014
CHEROKEE           1849
SUBURBAN           1815
TRAILBLAZER        1765
RAM                1605
CAMARO             1427
CARAVAN            1379
FOCUS              1227
CAVALIER           1188
CROWN VICTORIA     1165
                  ...  
LYNX                  2
LEMANS                2
PREMIER               2
GL                    2
SHADOW                2
MIGHTY MAX            2
MIRADA                2
TOWNSIDE MPV          2
BRONCO II             2
SPORTSMAN             2
TAXICAB               2
TREDIA                2
IMPULSE               2
TORONADO              2
SPIDER                1
STARION               1
CONCORD               1
GLC                   1
SPACECAB              1
NEWPORT               1
PRECIS                1
CATALINA              1
LASER                 1
STORM                 1
JUSTY                 1
FIERO                 1
MIDGET                1
GLF                   1
BRAT MPV              1
GF                    1
Name: VehicleModel, dtype: int64
FOUR DOOR             117402
SPORTS UTILITY         35725
PICK UP                24472
TWO DOOR               22607
MINIVAN                 5400
FULL SIZE VAN           2896
OTHER                   2891
STATION WAGON           1360
COMMERICAL TRUCK         763
MOTORCYLE                692
CONVERTIBLE              490
RECREATION VEHICLE       257
AIRCRAFT                  12
WATERCRAFT                10
Name: VehicleStyle, dtype: int64
WHITE              42122
BLACK              36753
SILVER             32022
GRAY               20790
BLUE               19963
RED                19581
GREEN              14859
GOLD                9303
TAN                 6250
MAROON              4166
BEIGE               3496
BROWN               2238
BURGUNDY            1385
YELLOW               995
ORANGE               903
PURPLE               615
STAINLESS STEEL      456
BRONZE               364
MULTICOLORED         243
CREAM                168
TEAL (GREEN)         162
PINK                  64
COPPER                57
CAMOUFLAGE            15
Name: VehicleColor, dtype: int64
CHEVROLET                     39856
FORD                          26939
NISSAN                        20923
TOYOTA                        20721
HONDA                         13464
DODGE                         13180
PONTIAC                       10493
GMC - GENERAL MOTORS CORP.     6343
JEEP                           5184
CHRYSLER                       4526
INFINITY                       4123
BUICK                          4000
KIA (4-WHEEL AUTOMOBILE)       3885
HYUNDAI                        3791
ACURA                          3788
MAZDA                          3326
SATURN                         3231
MITSUBISHI                     3121
OLDSMOBILE                     2787
CADILLAC                       2781
MERCEDES-BENZ                  2664
LEXUS                          2569
VOLKSWAGEN (VW)                2302
MERCURY                        2147
BMW                            2063
LINCOLN                        1886
BICYCLE                        1495
VOLVO                          1175
SUZUKI                          843
OTHER                           698
                              ...  
CITROEN                           3
LOTUS                             3
GRUMMAN-OLSEN                     3
CAPRI                             3
SHELBY                            3
ALLEGRO MOTOR HOME                2
MODEL 1300                        2
IVECO TRUCKS OF AMERICA           2
SWINGER                           2
AUTOCAR                           2
HOME MADE                         2
PEUGEOT                           2
BEEWD MOTOR HOME                  2
AUSTIN-HEALY                      2
WHITEGMC TRUCKS                   2
HOME MADE TRAILER                 2
MORGAN                            2
DAIHATSU                          1
BROCKWAY                          1
GO KART                           1
GINDY TRAILER                     1
OSHKOSH MOTOR TRUCK CO.           1
SKIPPER B. TRAILER CO.            1
DE LOREAN                         1
PINE TRAILER GROUP                1
AVANTI                            1
WHITE MOTOR CORP. PRE-1988        1
DORSEY TRAILERS, INC.             1
HOLMES WRECKER                    1
WARD LAFRANCE INTERNL             1
Name: VehicleMake, dtype: int64
LA    221486
TX      5551
MS      3560
FL      2259
CA      1775
GA      1758
AL      1404
TN      1307
NY       746
IL       695
NC       571
VA       433
MA       390
MO       390
PA       380
AR       357
OH       357
AZ       357
CO       340
SC       338
WA       316
MI       315
WI       281
MD       279
NJ       250
IN       246
OK       221
CT       207
OR       205
KY       183
NV       151
NM       147
KS       147
MN       136
IA        96
ME        60
NH        54
UT        51
DE        49
DC        48
HI        48
MT        48
NE        47
VT        47
RI        46
AK        43
WV        41
ID        35
WY        28
SD        25
ND        15
Name: SubjectDriverLicState, dtype: int64
In [9]:
fig = plt.figure(dpi = 100, figsize = (30, 60))

ax1 = fig.add_subplot(4, 2, 1)
ax1.plot = sns.countplot(stop_search["VehicleModel"])
plt.xticks(fontsize = 10)
plt.yticks(fontsize = 10)
plt.xlabel("Vehicle Model", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax2 = fig.add_subplot(4, 2, 2)
ax2.plot = sns.countplot(stop_search["VehicleStyle"])
plt.xticks(fontsize = 10)
plt.yticks(fontsize = 10)
plt.xlabel("Vehicle Style", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax3 = fig.add_subplot(4, 2, 3)
ax3.plot = sns.distplot(stop_search["VehicleYear"][~ np.isnan(stop_search["VehicleYear"])], bins = 50)
sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Vehicle Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

ax4 = fig.add_subplot(4, 2, 4)
ax4.plot = sns.countplot(stop_search["VehicleColor"])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Vehicle Color", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax5 = fig.add_subplot(4, 2, 5)
ax5.plot = sns.countplot(stop_search["VehicleMake"])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Vehicle Make", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax7 = fig.add_subplot(4, 2, 6)
ax7.plot = sns.countplot(stop_search["SubjectDriverLicState"])
plt.xticks(fontsize = 10)
plt.yticks(fontsize = 10)
plt.xlabel("Subject Driver License State", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

fig.show()
C:\Program Files\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

(3) Locations - Zip, BlockAddress, OfficerAssignment, Zone, District, Longitude, Latitude

In [10]:
print(stop_search["Zip"].value_counts())
print(stop_search["BlockAddress"].value_counts())
print(stop_search["OfficerAssignment"].value_counts())
print(stop_search["Zone"].value_counts())
print(stop_search["District"].value_counts())
70130.0    32992
70119.0    31795
70117.0    30545
70126.0    27388
70116.0    26811
70112.0    23844
70125.0    20369
70114.0    20346
70122.0    19397
70118.0    15354
70115.0    15065
70113.0    14856
70127.0    13497
70124.0    11088
70131.0     7946
70128.0     6319
70129.0     3064
70148.0      118
Name: Zip, dtype: int64
Saint Charles Ave & Calliope St      1599
004XX Bourbon St                     1540
007XX Canal St                       1451
Chef Menteur Hwy & Downman Rd        1272
005XX Bourbon St                     1271
003XX Bourbon St                     1262
001XX Royal St                       1224
002XX Bourbon St                     1027
Calliope St & Saint Charles Ave       977
Franklin Ave & Saint Claude Ave       811
024XX Sanctuary Dr                    768
011XX Elysian Fields Ave              729
Chef Menteur Hwy & Desire Pkwy        682
006XX Bourbon St                      674
060XX Chef Menteur Hwy                672
001XX Bourbon St                      670
008XX Canal St                        626
027XX Tulane Ave                      604
S Carrollton Ave & Earhart Blvd       570
Elysian Fields Ave & Florida Ave      543
Elysian Fields Ave & Saint Claud      534
060XX Stars And Stripes Blvd          525
009XX Canal St                        508
N Claiborne Ave & Elysian Fields      498
S Carrollton Ave & Tulane Ave         497
Bourbon St & Conti St                 493
006XX Canal St                        477
005XX Canal St                        474
Press St & Saint Claude Ave           471
003XX Royal St                        447
                                     ... 
Paris Ave (3X03) & Gentilly Blvd        1
015XX 2nd St                            1
Picheloup Pl & Delgado Ave              1
Chase St & Gus St                       1
St Charles Ave & Clio St                1
Lafon Dr & Ransom St                    1
019XX N Derbigny St                     1
Dauphine St & Louisa St                 1
I610 W & I-10 E Exit                    1
050XX Prytania St                       1
Marque Dr & Chef Menteur Blv            1
074XX Freret St                         1
Franklin Ave (5M01) & N Galvez St       1
Canal S & S Lopez St                    1
044XX N Rampart St                      1
074XX Northgate Dr                      1
N White St & Dumaine St                 1
N Claiborne Av & Tennessee St           1
Millaudon St & Saint Charles Ave        1
Tchoupitoulas St & US90B W              1
Iroquois St & Tecumseh St               1
Grape St & Vendome Pl                   1
110XX Yardley Rd                        1
Paris Ave & Charlton Dr                 1
Ransom St & Majestic Oaks Dr            1
102XX Seawood St                        1
076XX Sandy Cove Dr                     1
137XX Lourdes St                        1
Moss St & Saint Ann St                  1
009XX N Villere                         1
Name: BlockAddress, dtype: int64
8th  District     85567
3rd  District     62900
7th  District     48792
5th  District     46358
6th  District     43680
2nd  District     39732
1st District      35093
4th  District     34985
Traffic           22795
SOD                4941
ISB                3834
MSB                 453
FOB                 398
Reserve             353
PIB                  88
Superintendent       40
NCIC                 24
Name: OfficerAssignment, dtype: int64
D    53196
C    41879
E    33081
I    32990
F    25286
A    24224
B    23233
G    21914
H    21429
K    21168
J    20932
L    18178
Q    13060
U    12848
M    12499
T     9904
R     8522
O     7732
P     6363
N     5410
S     4978
X     4878
V     3135
Y     2581
W      737
Name: Zone, dtype: int64
8    87227
3    65176
7    54497
6    52069
5    51221
2    41862
1    40613
4    37492
Name: District, dtype: int64
In [11]:
fig = plt.figure(dpi = 100, figsize = (30, 60))

ax1 = fig.add_subplot(4, 2, 1)
ax1.plot = sns.distplot(stop_search["Zip"][~ np.isnan(stop_search["Zip"])])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Zip Code", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax2 = fig.add_subplot(4, 2, 2)
ax2.plot = sns.countplot(stop_search["OfficerAssignment"])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Officer Assignment", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax4 = fig.add_subplot(4, 2, 3)
ax4.plot = sns.countplot(stop_search["Zone"])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Zone", fontsize = 40)
plt.ylabel("Count", fontsize = 40)

ax5 = fig.add_subplot(4, 2, 4)
ax5.plot = sns.countplot(stop_search["District"])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("District", fontsize = 40)
plt.ylabel("Count", fontsize = 40)
                                     
ax3 = fig.add_subplot(4, 2, 5)
ax3.plot = sns.distplot(stop_search["Longitude"][~ np.isnan(stop_search["Longitude"])], bins = 100)
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Longitude", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)
                                    
ax3 = fig.add_subplot(4, 2, 6)
ax3.plot = sns.distplot(stop_search["Latitude"][~ np.isnan(stop_search["Latitude"])], bins = 100)
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Latitude", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

fig.show()
C:\Program Files\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

(4) Time - LastModifiedDateTime, CreatedDateTime, EventDate

In [12]:
# LastModifiedDateTime
LastModifiedDateTime = pd.to_datetime(stop_search['LastModifiedDateTime'])
LastModifiedYear = LastModifiedDateTime.dt.year
LastModifiedMonth = LastModifiedDateTime.dt.month
LastModifiedDay = LastModifiedDateTime.dt.day
LastModifiedDayofYear = LastModifiedDateTime.dt.dayofyear
LastModifiedDayofWeek = LastModifiedDateTime.dt.dayofweek
LastModifiedWeekofYear = LastModifiedDateTime.dt.weekofyear
LastModifiedQuarter = LastModifiedDateTime.dt.quarter
In [13]:
fig = plt.figure(dpi = 100, figsize = (30, 60))

ax1 = fig.add_subplot(4, 2, 1)
ax1.plot = sns.distplot(LastModifiedYear[~ np.isnan(LastModifiedYear)])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Last Modified Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax2 = fig.add_subplot(4, 2, 2)
ax2.plot = sns.distplot(LastModifiedMonth[~ np.isnan(LastModifiedMonth)])
plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.xlabel("Last Modified Month", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax4 = fig.add_subplot(4, 2, 3)
ax4.plot = sns.distplot(LastModifiedDay[~ np.isnan(LastModifiedDay)])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Last Modified Day", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax5 = fig.add_subplot(4, 2, 4)
ax5.plot = sns.distplot(LastModifiedDayofYear[~ np.isnan(LastModifiedDayofYear)])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Last Modified Day of Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)
                                     
ax3 = fig.add_subplot(4, 2, 5)
ax3.plot = sns.distplot(LastModifiedDayofWeek[~ np.isnan(LastModifiedDayofWeek)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Last Modified Day of Week", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)
                                    
ax3 = fig.add_subplot(4, 2, 6)
ax3.plot = sns.distplot(LastModifiedWeekofYear[~ np.isnan(LastModifiedWeekofYear)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Last Modified Week of Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

ax3 = fig.add_subplot(4, 2, 7)
ax3.plot = sns.distplot(LastModifiedQuarter[~ np.isnan(LastModifiedQuarter)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Last Modified Quarter", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

fig.show()
C:\Program Files\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "
In [14]:
# CreatedDateTime
CreatedDateTime = pd.to_datetime(stop_search['CreatedDateTime'])
CreatedYear = CreatedDateTime.dt.year
CreatedMonth = CreatedDateTime.dt.month
CreatedDay = CreatedDateTime.dt.day
CreatedDayofYear = CreatedDateTime.dt.dayofyear
CreatedDayofWeek = CreatedDateTime.dt.dayofweek
CreatedWeekofYear = CreatedDateTime.dt.weekofyear
CreatedQuarter = CreatedDateTime.dt.quarter
In [15]:
fig = plt.figure(dpi = 100, figsize = (30, 60))

ax1 = fig.add_subplot(4, 2, 1)
ax1.plot = sns.distplot(CreatedYear[~ np.isnan(CreatedYear)])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Created Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax2 = fig.add_subplot(4, 2, 2)
ax2.plot = sns.distplot(CreatedMonth[~ np.isnan(CreatedMonth)])
plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.xlabel("Created Month", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax4 = fig.add_subplot(4, 2, 3)
ax4.plot = sns.distplot(CreatedDay[~ np.isnan(CreatedDay)])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Created Day", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax5 = fig.add_subplot(4, 2, 4)
ax5.plot = sns.distplot(CreatedDayofYear[~ np.isnan(CreatedDayofYear)])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Created Day of Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)
                                     
ax3 = fig.add_subplot(4, 2, 5)
ax3.plot = sns.distplot(CreatedDayofWeek[~ np.isnan(CreatedDayofWeek)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Created Day of Week", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)
                                    
ax3 = fig.add_subplot(4, 2, 6)
ax3.plot = sns.distplot(CreatedWeekofYear[~ np.isnan(CreatedWeekofYear)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Created Week of Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

ax3 = fig.add_subplot(4, 2, 7)
ax3.plot = sns.distplot(CreatedQuarter[~ np.isnan(CreatedQuarter)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Created Quarter", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

fig.show()
C:\Program Files\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "
In [89]:
EventWeekofYear.value_counts()
Out[89]:
2     9913
4     9472
34    9379
3     9174
33    9145
32    9095
25    9042
24    8715
23    8709
38    8693
26    8690
21    8687
35    8649
39    8643
37    8576
41    8570
36    8563
5     8560
46    8474
28    8462
1     8452
40    8376
42    8352
31    8339
17    8316
29    8304
20    8290
50    8259
12    8240
30    8223
43    8169
27    8132
49    8127
11    8125
45    8102
15    8076
13    8054
18    8021
19    7897
51    7895
48    7860
47    7843
16    7778
14    7775
44    7733
22    7634
9     7509
52    7127
6     6926
7     6837
10    6654
8     6495
53    1026
Name: EventDate, dtype: int64
In [16]:
# EventDate
EventDate = pd.to_datetime(stop_search['EventDate'])
EventYear = EventDate.dt.year
EventMonth = EventDate.dt.month
EventDay = EventDate.dt.day
EventDayofYear = EventDate.dt.dayofyear
EventDayofWeek = EventDate.dt.dayofweek
EventWeekofYear = EventDate.dt.weekofyear
EventQuarter = EventDate.dt.quarter
EventHour = EventDate.dt.hour
EventMinute = EventDate.dt.minute
In [34]:
fig = plt.figure(dpi = 100, figsize = (50, 50))

ax1 = fig.add_subplot(3, 3, 1)
ax1.plot = sns.distplot(EventYear[~ np.isnan(EventYear)])
sns.plt.xlim(2010, 2016)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Event Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax2 = fig.add_subplot(3, 3, 2)
ax2.plot = sns.distplot(EventMonth[~ np.isnan(EventMonth)])
plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.xlabel("Event Month", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax3 = fig.add_subplot(3, 3, 3)
ax3.plot = sns.distplot(EventDay[~ np.isnan(EventDay)])
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Event Day", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)

ax4 = fig.add_subplot(3, 3, 4)
ax4.plot = sns.distplot(EventDayofYear[~ np.isnan(EventDayofYear)])
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Event Day of Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 40)
                                     
ax5 = fig.add_subplot(3, 3, 5)
ax5.plot = sns.distplot(EventDayofWeek[~ np.isnan(EventDayofWeek)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Event Day of Week", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)
                                    
ax6 = fig.add_subplot(3, 3, 6)
ax6.plot = sns.distplot(EventWeekofYear[~ np.isnan(EventWeekofYear)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel("Event Week of Year", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

ax7 = fig.add_subplot(3, 3, 7)
ax7.plot = sns.distplot(EventQuarter[~ np.isnan(EventQuarter)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Event Quarter", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

ax8 = fig.add_subplot(3, 3, 8)
ax8.plot = sns.distplot(EventHour[~ np.isnan(EventHour)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Event Hour", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

ax9 = fig.add_subplot(3, 3, 9)
ax9.plot = sns.distplot(EventMinute[~ np.isnan(EventMinute)])
# sns.plt.xlim(1975, 2020)
plt.xticks(fontsize = 25)
plt.yticks(fontsize = 25)
plt.xlabel("Event Minute", fontsize = 40)
plt.ylabel("Probability", fontsize = 50)

fig.show()
C:\Program Files\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

(5) StopType - StopDescription

In [18]:
print(stop_search["StopDescription"].value_counts())

plot = sns.countplot(stop_search["StopDescription"])

for label in plot.get_xticklabels():
    label.set_rotation(60)

plt.xticks(fontsize = 10)
plt.yticks(fontsize = 10)
plt.xlabel("Stop Type", fontsize = 30)
plt.ylabel("Count", fontsize = 30)
TRAFFIC VIOLATION         238448
CALL FOR SERVICE           60341
SUSPECT PERSON             57659
CRIMINAL VIOLATION         24970
OTHER                      21950
CITIZEN CONTACT            14184
SUSPECT VEHICLE             5144
FLAGGED DOWN                3789
JUVENILE VIOLATION          2970
PRESENT AT CRIME SCENE       702
Name: StopDescription, dtype: int64
Out[18]:
<matplotlib.text.Text at 0x1eab534ca58>

(6) Stop result - ActionsTaken

In [19]:
print(stop_search["ActionsTaken"].value_counts())
Stop Results: Citation issued;Subject Type: Driver;Search Occurred: No;Evidence Seized: No                                                                                                                                                                                                                                                                               11536
Stop Results: Citation issued;Subject Type: Driver                                                                                                                                                                                                                                                                                                                        9923
Stop Results: Verbal warning;Subject Type: Driver;Search Occurred: No;Evidence Seized: No                                                                                                                                                                                                                                                                                 8543
Stop Results: Citation issued;Subject Type: Driver;Search Occurred: No                                                                                                                                                                                                                                                                                                    7495
Stop Results: No action taken                                                                                                                                                                                                                                                                                                                                             6806
Stop Results: Verbal warning                                                                                                                                                                                                                                                                                                                                              6440
Stop Results: Verbal warning;Subject Type: Driver                                                                                                                                                                                                                                                                                                                         6290
Stop Results: Citation issued                                                                                                                                                                                                                                                                                                                                             5919
Stop Results: Verbal warning;Subject Type: Driver;Search Occurred: No                                                                                                                                                                                                                                                                                                     5788
Stop Results: No action taken;Search Occurred: No;Evidence Seized: No                                                                                                                                                                                                                                                                                                     4495
Stop Results: Citation issued;Subject Type: Driver;Search Occurred: No;Legal Basises: Probable cause;Evidence Seized: No                                                                                                                                                                                                                                                  4225
Stop Results: Verbal warning;Search Occurred: No;Evidence Seized: No                                                                                                                                                                                                                                                                                                      4158
Stop Results: Arrest made                                                                                                                                                                                                                                                                                                                                                 3622
Stop Results: Verbal warning;Search Occurred: No                                                                                                                                                                                                                                                                                                                          3308
Stop Results: Citation issued;Search Occurred: No;Evidence Seized: No                                                                                                                                                                                                                                                                                                     3122
Stop Results: No action taken;Search Occurred: No                                                                                                                                                                                                                                                                                                                         2438
Stop Results: Arrest made;Search Occurred: Yes;Search Types: Pat-down;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                                                                                               2223
Stop Results: Verbal warning;Subject Type: Driver;Search Occurred: No;Legal Basises: Probable cause;Evidence Seized: No                                                                                                                                                                                                                                                   2029
Stop Results: Arrest made;Search Occurred: Yes;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                                                                                                                      1836
Stop Results: Citation issued;Search Occurred: No                                                                                                                                                                                                                                                                                                                         1407
Stop Results: Arrest made;Search Occurred: No;Evidence Seized: No                                                                                                                                                                                                                                                                                                         1244
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver                                                                                                                                                                                                                                                                                               934
Stop Results: Verbal warning;Search Occurred: No;Legal Basises: Probable cause;Evidence Seized: No                                                                                                                                                                                                                                                                         882
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Pat-down;Search Types: Driver;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                        815
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Driver;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                                               801
Stop Results: Citation issued;Search Occurred: No;Legal Basises: Probable cause;Evidence Seized: No                                                                                                                                                                                                                                                                        743
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Pat-down;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                                             703
Stop Results: No action taken;Search Occurred: No;Legal Basises: Probable cause;Evidence Seized: No                                                                                                                                                                                                                                                                        700
Stop Results: Citation issued;Subject Type: Driver;Search Occurred: No;Legal Basises: Probable cause                                                                                                                                                                                                                                                                       548
Stop Results: Arrest made;Search Occurred: No                                                                                                                                                                                                                                                                                                                              543
                                                                                                                                                                                                                                                                                                                                                                         ...  
Stop Results: Citation issued;Subject Type: Driver;Legal Basises: Plain view;Evidence Seized: Yes;Evidence Types: Weapon(s)                                                                                                                                                                                                                                                  1
Stop Results: No action taken;Subject Type: Passenger;Search Occurred: Yes;Search Types: Vehicle;Search Types: Pat-down;Search Types: Passenger(s);Legal Basises: Consent to search;Evidence Seized: No                                                                                                                                                                      1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: No;Search Types: Pat-down;Legal Basises: Probable cause;Legal Basises: Incident to arrest                                                                                                                                                                                      1
Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Pat-down;Search Types: Driver;Legal Basises: Warrant;Legal Basises: Incident to arrest                                                                                                                                                                                                     1
Search Occurred: Yes;Search Types: Driver;Evidence Seized: No                                                                                                                                                                                                                                                                                                                1
Stop Results: Arrest made;Subject Type: Driver;Search Types: Pat-down;Search Types: Driver;Legal Basises: Warrant                                                                                                                                                                                                                                                            1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Legal Basises: Probable cause;Evidence Seized: Yes;Evidence Types: Drugs                                                                                                                                                                                                                        1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Types: Pat-down;Legal Basises: Plain view;Evidence Seized: Yes;Evidence Types: Drugs                                                                                                                                                                                                     1
Stop Results: Arrest made;Subject Type: Driver;Subject Type: Passenger;Search Occurred: Yes;Search Types: Vehicle;Search Types: Pat-down;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                               1
Stop Results: Citation issued;Subject Type: Driver;Search Occurred: No;Evidence Seized: No;Evidence Types: Other                                                                                                                                                                                                                                                             1
Stop Results: Arrest made;Search Occurred: Yes;Search Types: Driver;Legal Basises: Incident to arrest;Evidence Seized: Yes;Evidence Types: Other                                                                                                                                                                                                                             1
Stop Results: Arrest made;Subject Type: Driver;Legal Basises: Probable cause;Legal Basises: Plain view                                                                                                                                                                                                                                                                       1
Stop Results: Citation issued;Search Types: Driver;Legal Basises: Probable cause;Evidence Seized: No                                                                                                                                                                                                                                                                         1
Stop Results: Arrest made;Search Occurred: Yes;Search Types: Pat-down;Legal Basises: Warrant;Legal Basises: Incident to arrest;Evidence Seized: Yes;Evidence Types: Weapon(s);Evidence Types: Other                                                                                                                                                                          1
Stop Results: Verbal warning;Subject Type: Passenger;Search Occurred: No;Legal Basises: Inventory;Evidence Seized: No                                                                                                                                                                                                                                                        1
Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Vehicle;Search Types: Pat-down;Search Types: Driver;Search Types: Passenger(s);Legal Basises: Warrant;Legal Basises: Probable cause;Evidence Seized: No                                                                                                                                    1
Stop Results: Citation issued;Subject Type: Driver;Search Occurred: No;Search Types: Pat-down;Legal Basises: Warrant                                                                                                                                                                                                                                                         1
Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Vehicle;Search Types: Pat-down;Legal Basises: Probable cause;Legal Basises: Plain view;Evidence Seized: Yes;Evidence Types: Weapon(s)                                                                                                                                                      1
Stop Results: Citation issued;Search Occurred: Yes;Legal Basises: Plain view;Evidence Seized: No;Evidence Types: Weapon(s);Evidence Types: Other                                                                                                                                                                                                                             1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Types: Pat-down;Search Types: Driver;Legal Basises: Probable cause;Legal Basises: Incident to arrest                                                                                                                                                                                     1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Pat-down;Legal Basises: Consent to search;Legal Basises: Warrant;Legal Basises: Probable cause;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                         1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Legal Basises: Probable cause;Legal Basises: Incident to arrest                                                                                                                                                                                                            1
Stop Results: Arrest made;Search Occurred: Yes;Search Types: Pat-down;Legal Basises: Consent to search;Legal Basises: Plain view;Evidence Seized: Yes;Evidence Types: Drugs                                                                                                                                                                                                  1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Subject Type: Passenger;Search Occurred: Yes;Search Types: Vehicle;Search Types: Driver;Search Types: Passenger(s);Legal Basises: Consent to search;Legal Basises: Probable cause;Legal Basises: Incident to arrest;Evidence Seized: Yes;Evidence Types: Weapon(s);Evidence Types: Drugs        1
Search Occurred: Yes;Search Types: Pat-down;Legal Basises: Probable cause;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                                                                                              1
Stop Results: Arrest made;Subject Type: Passenger;Search Occurred: Yes;Search Types: Vehicle;Legal Basises: Incident to arrest;Evidence Seized: Yes;Evidence Types: Drugs                                                                                                                                                                                                    1
Stop Results: No action taken;Subject Type: Passenger;Search Occurred: Yes;Search Types: Vehicle;Search Types: Pat-down;Search Types: Passenger(s)                                                                                                                                                                                                                           1
Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Driver;Legal Basises: Probable cause;Evidence Seized: Yes;Evidence Types: Drugs                                                                                                                                                                                                            1
Stop Results: Verbal warning;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: No;Search Types: Driver;Legal Basises: Incident to arrest;Evidence Seized: No                                                                                                                                                                                                   1
Stop Results: Citation issued;Stop Results: Arrest made;Subject Type: Driver;Search Occurred: Yes;Search Types: Vehicle;Search Types: Pat-down;Search Types: Driver;Legal Basises: Warrant;Legal Basises: Incident to arrest;Evidence Seized: Yes;Evidence Types: Other                                                                                                      1
Name: ActionsTaken, dtype: int64

(7) Time series analysis - EventData

In [35]:
event_time = pd.DataFrame({'Year': EventYear, 
                           'Quarter': EventQuarter,
                           'Month': EventMonth,
                           'WeekofYear': EventWeekofYear,
                           'Day': EventDay,
                           'DayofYear': EventDayofYear,
                           'DayofWeek': EventDayofWeek,
                           'Hour': EventHour,
                           'Minute': EventMinute,
                           'Number': np.repeat(1, stop_search.shape[0])
                          }, index = None)
In [36]:
event_time.head()
Out[36]:
Day DayofWeek DayofYear Hour Minute Month Number Quarter WeekofYear Year
0 1 4 1 1 11 1 1 1 53 2010
1 1 4 1 2 6 1 1 1 53 2010
2 1 4 1 2 6 1 1 1 53 2010
3 1 4 1 2 6 1 1 1 53 2010
4 1 4 1 2 6 1 1 1 53 2010
In [81]:
# aggregate data by year, month, day
ymd = pd.to_datetime(event_time.Year*10000 + event_time.Month*100 + event_time.Day, format='%Y%m%d')
ymd_data = pd.DataFrame({'Date': ymd, 
                         'Number': np.repeat(1, stop_search.shape[0])
                        }, index = None)
agg_ymd = ymd_data.groupby(['Date'])['Number'].sum()
index = agg_ymd.index
agg_ymd = pd.DataFrame({'Count': agg_ymd
                        # 'Date': index
                       }, index = None)
# agg_ymd.Date = pd.to_datetime(agg_ymd.Date)
agg_ymd.set_index(index)
print(ymd_data.shape)
print(ymd_data.head())
print(agg_ymd.shape)
print(agg_ymd.head())
(430157, 2)
        Date  Number
0 2010-01-01       1
1 2010-01-01       1
2 2010-01-01       1
3 2010-01-01       1
4 2010-01-01       1
(2739, 1)
            Count
Date             
1991-07-24      2
1999-11-03      3
1999-11-21      1
2001-03-03      1
2001-06-10      1
In [82]:
from pandas import Series
timeData = Series(agg_ymd.Count, index=index)
timeData['2010':'2016'].plot()
Out[82]:
<matplotlib.axes._subplots.AxesSubplot at 0x1eabafa9e80>
In [83]:
timeData['2016-01-01':'2016-12-31'].plot()
Out[83]:
<matplotlib.axes._subplots.AxesSubplot at 0x1eab53a4e48>
In [85]:
timeData['2016-12-01':'2016-12-31'].plot()
Out[85]:
<matplotlib.axes._subplots.AxesSubplot at 0x1eab4ada668>
In [87]:
timeData[timeData > 400]
Out[87]:
Date
2011-01-13    426
2011-01-14    405
2011-01-28    402
2011-02-02    414
2012-03-20    407
Name: Count, dtype: int32
In [ ]: